<?php


namespace App\Http\Controllers\App\V3;


//房间互动 生效 变声 玩法 道具 有关api

use App\Enums\RedisKeyEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Services\RedisServices;
use Illuminate\Http\Request;
use App\Response\Response;

class InteractionController extends BaseController
{

    public function iconList(Request $request)
    {


        $param = $request->input();

        $v = app('validator')->make(
            $param,
            [
                'page' => 'filled|integer|min:1',
                'perPage' => 'filled|integer|min:1',
                'type' => 'filled|in:1,2,3,4'
            ]
        );
        $param['page'] = $param['page'] ?? 1;
        $param['perPage'] = $param['perPage'] ?? 8;
        $param['type'] = $param['type'] ?? 1;
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        // 获取配置 是否 这用户ID  不给他看见游戏
        $user_id_arr = explode(',',app('model_config')->getValue('GAME_IS_USER')) ?? [];
        if(in_array(UID,$user_id_arr)){
            return Response::sendData([
                'data'  => [],
                'total' => 0,
            ]);
        }
//        if(in_array($user_id,){
//            return Response::sendData([]);
//        }
        $list = app('repo_room_interaction')->getIconList(UID, $param['page'], $param['perPage'], $param['type']);
        return Response::sendData($list);
    }

    public function canUseInteraction(Request $request)
    {
        $res = app('model_config')->getValue("INTERACTION_SWITCH");
        if ($res != 1) {
            throw new BasicException(10010, '互动模块已关闭');
        }
        return Response::sendData($res);
    }

    //声音互动
    public function sendsound(Request $request)
    {
        $room_id = $request->input('room_id');
        $sound_url = $request->input('sound_url', '');

        if (abs(floor($room_id)) != $room_id) {
            throw new BasicException(10010);
        };

        // 判断是否开麦
        $userPitInfo = app('model_room_pit')
            ->where([ ['room_id', '=', $room_id],  ['user_id', '=', UID] ])
            ->selectRaw('room_id, user_id, pit_number, voice, shutup')
            ->first();
        if(empty($userPitInfo)){
            throw new BasicException(0, '请先上麦');
        }
        if($userPitInfo['voice'] == 0){
            throw new BasicException(0, '请先开麦');
        }
        if($userPitInfo['shutup'] == 1){
            throw new BasicException(0, '您当前为禁麦状态，无法发送音效');
        }

        app('repo_room_interaction')->pushInteractionSoundRoom(UID, $room_id, $sound_url);
        $data = [
            'room_id' => $room_id,
            'user_id' => UID,
            'sound_url' => $sound_url
        ];
        return Response::sendData($data);
    }
}
